package com.morehair.controller.wx;

import com.morehair.bean.bo.LoginUser;
import com.morehair.bean.bo.wx.SmsBO;
import com.morehair.bean.bo.wx.WxRegisterBO;
import com.morehair.bean.pojo.User;
import com.morehair.bean.vo.admin.BaseRespVo;

import com.morehair.bean.vo.wx.LoginVo;
import com.morehair.bean.vo.wx.WxResetPasswordVO;
import com.morehair.config.MallToken;
import com.morehair.service.wx.WxAuthService;
import com.morehair.utils.GetNowUser;

import lombok.Data;

import com.morehair.utils.Md5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

import java.security.NoSuchAlgorithmException;

/**
 * @Description:
 * @Author raohui
 * @Date 2021/6/8 10:02
 */
@Data
@RestController
@RequestMapping("wx/auth")
public class WxAuthController {

    private String generatedCode;
    @Autowired
    WxAuthService wxAuthService;

    @RequestMapping("regCaptcha")
    public BaseRespVo getSms(@RequestBody SmsBO smsBO) {
//        Session session = SecurityUtils.getSubject().getSession();
        generatedCode = wxAuthService.getSms(smsBO);
//        session.setAttribute("generatedCode", generatedCode);
//        generatedCode = ((String) session.getAttribute("generatedCode"));
        return BaseRespVo.ok("验证码发送成功");
    }


    @RequestMapping("register")
    public BaseRespVo wxRegister(@RequestBody WxRegisterBO registerBO) throws NoSuchAlgorithmException {
//        Session session = SecurityUtils.getSubject().getSession();
//        generatedCode = ((String) session.getAttribute("generatedCode"));
//        generatedCode = "123456";
        Integer stat = wxAuthService.wxRegister(registerBO, generatedCode);
//        session.removeAttribute("generatedCode");
        if (stat == 0) {
            return BaseRespVo.fail("验证码不正确");
        } else if (stat == 1) {
            return BaseRespVo.fail("用户已存在");
        } else if (stat == 3) {
            return BaseRespVo.fail("该手机号已经注册过");
        }

        return BaseRespVo.ok("注册成功");
    }

    @PostMapping("reset")
    public BaseRespVo resetPassword(@RequestBody WxResetPasswordVO wxResetPasswordVO, String generatedCode) throws NoSuchAlgorithmException {
        generatedCode = "123456";////////////////////////////////////////////
        Integer stat = wxAuthService.resetPassword(wxResetPasswordVO, generatedCode);
        if (stat == 0) {
            return BaseRespVo.fail("验证码不正确");
        }
        return BaseRespVo.ok("修改密码成功");
    }

    @PostMapping("login")
    public BaseRespVo login(@RequestBody LoginUser loginUser) {

        try {
            String username = loginUser.getUsername();
            String password = Md5Util.getMd5(loginUser.getPassword());
            Subject subject = SecurityUtils.getSubject();
            subject.login(new MallToken(username, password,"wx"));
            LoginVo loginVo = wxAuthService.login(username);
            return BaseRespVo.ok(loginVo);

        } catch (AuthenticationException | NoSuchAlgorithmException e) {
            return BaseRespVo.fail(700,null,"用户帐号或密码不正确");
//            {"errno":605,"errmsg":"用户帐号或密码不正确"}
        }

    }

    @PostMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }
}
